home *** CD-ROM | disk | FTP | other *** search
/ Aminet 28 / Aminet 28 (1998)(GTI - Schatztruhe)[!][Dec 1998].iso / Aminet / comm / tcp / rxsocket.lha / rxsocket / examples / echottcp.rexx < prev    next >
Encoding:
OS/2 REXX Batch file  |  1998-10-27  |  1.6 KB  |  72 lines

  1. /*
  2.     A very simple echo T/TCP client.
  3.     Show how to make a basic connection to a tcp service
  4.     in T/TCP and came back to TCP if it doesn't work.
  5.     To test it on localhost, be sure echo/tcp is enabeld
  6.     in the services and inetd database, then write
  7.     rx echotcp localhost.
  8. */
  9.  
  10. if ~show("L","rexxsupport.library") then
  11.     if ~addlib("rexxsupport.library",0,-30) then do
  12.         say "no rexxsupport.library"
  13.         exit
  14.     end
  15. if ~show("L","rxsocket.library") then
  16.     if ~addlib("rxsocket.library",0,-30) then do
  17.         say "no rxsocket.library"
  18.         exit
  19.     end
  20. if ~show("L","rmh.library") then
  21.     if ~addlib("rmh.library",0,-30) then do
  22.         say "no rmh.library"
  23.         exit
  24.     end
  25.  
  26. prg = ProgramName("NOEXT")
  27.  
  28. if ~RMH_ReadArgs("HOST/A") then do
  29.     call PrintFault(IoErr(),prg)
  30.     exit
  31. end
  32.  
  33. addr = resolve(parm.0.value)
  34. if addr=="-1" then call err "no host <"parm.0.value">"
  35.  
  36. if ~getservbyname("SE","echo","tcp") then
  37.     call err "echo tcp service not found"
  38.  
  39. sin.AddrFamily = "INET"
  40. sin.AddrAddr   = addr
  41. sin.AddrPort   = se.ServPort
  42.  
  43. sock = socket("INET","STREAM","IP")
  44. if sock<0 then call err "no socket ("errno()")"
  45.  
  46. request = "echo service test"
  47.  
  48. dottcp=1
  49.  
  50. if sendto(sock,REQUEST,"EOF","SIN")~=length(REQUEST) then do
  51.     err=Errno()
  52.     if err==57 then dottcp=0
  53.     else call err "send error ("errno()")"
  54. end
  55.  
  56. if dottcp==0 then do
  57.     say "(back to TCP)"
  58.     if connect(sock,"SIN")<0 then call err "connect error ("errno()")"
  59.     if send(sock,REQUEST)~=length(REQUEST) then call err "send error ("errno()")"
  60. end
  61.  
  62. if recv(sock,"BUF",256)<0 then
  63.     call err "recv error ("errno()")"
  64.  
  65. say buf
  66. exit
  67.  
  68. err: procedure expose prg
  69. parse arg msg
  70.     say prg":" msg
  71.     exit
  72.